<?php
/**
 *
 *幼儿心得控制器
 *
 **/

namespace Admin\Controller;

class ChildExperienceController extends ComController
{

    /*
     * 添加幼儿心得
     */
    public  function  add(){

        $uid = session('uid');
        $member = M('member');
        $user= $member->where('uid ='.$uid)->find();

        if($user['level'] == 1){

               $sc_id=$uid;

        }else if($user['level']  ==2){

               $sc_id = $user['pid'];

        }else{

               echo '';

        }

        if(IS_POST){

                $title =   I('post.title') ? I('post.title') : '';
                $content =   I('post.content') ? I('post.content') : '';

                if(!$title) $this->error('请填写幼儿心得标题');
                if(!$content) $this->error('请填写幼儿心得内容');

                $data['title']=$title;
                $data['content']=$content;
                $data['sc_id']=$sc_id;
                $data['add_time']=time();
                $data['update_time']=time();

                $child_experience = M('child_experience');
                $result= $child_experience->add($data);

                if($result){
                    $this->success('添加幼儿心得成功！',U('index'));
                }else{
                    $this->error('添加幼儿心得失败!');
                }

        }else{

            $this->display();

        }


    }


    /*
     * 幼儿心得列表
     */
    public   function   index(){

        $uid = session('uid');
        $member = M('member');
        $user= $member->where('uid ='.$uid)->find();

        if($user['level'] == 1){

            $sc_id=$uid;

        }else if($user['level']  ==2){

            $sc_id = $user['pid'];

        }else{

            echo '';

        }

        //搜索分页
        $p= intval($_GET['p'])?$_GET['p']:1;
        $child_experience = M('child_experience');
        $pagesize = 2;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $prefix = C('DB_PREFIX');

        $where = 'sc_id ='. $sc_id;

        //幼儿心得标题
        $title = isset($_GET['title']) ? htmlentities($_GET['title']) : '';

        if ($title) {
            $where .= "  and  title like '%{$title}%' ";
        }

        $data= $child_experience->where($where)
            ->order('id asc' )
            ->limit($offset . ',' . $pagesize)
            ->select();

        $count=$child_experience->where($where)->count();

        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();

        $this->assign('data',$data);
        $this->assign('page', $page);
        $this->display();

    }


    /*
     * 修改幼儿心得
     */
    public  function  edit(){

        $uid = session('uid');
        $member = M('member');
        $user= $member->where('uid ='.$uid)->find();

        if($user['level'] == 1){

            $sc_id=$uid;

        }else if($user['level']  ==2){

            $sc_id = $user['pid'];

        }else{

            echo '';

        }

        $id = I('get.id') ? I('get.id') : 0;

        if(IS_POST){

                    $child_experience = M('child_experience');
                    $res = $child_experience->where('id ='.$id  and 'sc_id ='.$sc_id)->find();

                    if($res){

                        $param['title'] =  I('post.title');
                        $param['content'] =  I('post.content');
                        $param['update_time'] = time();

                        $child_experience = M('child_experience');
                        $child_experience->where(array('id'=>$id,'sc_id'=>$sc_id))->save($param);

                        $this->success('修改幼儿心得成功！',U('index'));die;

                    }else{

                        $this->error('修改幼儿心得失败，参数错误！');

                    }

        }else{

            $data =  M('child_experience')->where('id ='.$id)->find();

            $this->assign('data',$data);
            $this->display();

        }


    }


    /*
     * 删除幼儿心得
     */
    public  function  del(){

        $uid = session('uid');
        $member = M('member');
        $user= $member->where('uid ='.$uid)->find();

        if($user['level'] == 1){

            $sc_id=$uid;

        }else if($user['level']  ==2){

            $sc_id = $user['pid'];

        }else{

            echo '';

        }

        $id = I('get.id') ? I('get.id') : 0;

        $child_experience = M('child_experience');
        $data = $child_experience->where('id ='.$id  and 'sc_id ='.$sc_id)->find();

        if($data){

            $res = $child_experience->where(array('id'=>$id,'sc_id'=>$sc_id))->delete();

            if($res){
                $this->success('删除幼儿心得，成功!',U('index'));
            }else{
                $this->error('删除幼儿心得，失败!'.$child_experience->getError());
            }

        }else{

            $this->error('删除幼儿心得失败，没有找到该记录，参数错误！');

        }


    }
















}